package familytree.yu.com.admin.web.service.memberinfo;

import familytree.yu.com.admin.api.entity.memberinfo.BaseMemberLoginInfoEntity;
import familytree.yu.com.admin.api.repository.BaseMemberLoginInfoRepository;
import familytree.yu.com.admin.api.service.memberinfo.IBaseMemberLoginInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @Author: yuyunbo
 * @Date: 2021/2/8 21:50
 * @Description:
 */
@Service
public class BaseMemberLoginInfoServiceImpl implements IBaseMemberLoginInfoService {
    @Autowired
    private BaseMemberLoginInfoRepository baseMemberLoginInfoRepository;


    /**
     * 保存会员登录信息
     *
     * @param baseMemberLoginInfoEntity
     * @return
     */
    @Override
    public Long saveBaseMemberLoginInfo(BaseMemberLoginInfoEntity baseMemberLoginInfoEntity) {
        if (baseMemberLoginInfoEntity == null) {
            return null;
        }
        baseMemberLoginInfoEntity.setGmtCreated(new Date());
        baseMemberLoginInfoRepository.save(baseMemberLoginInfoEntity);
        return baseMemberLoginInfoEntity.getId();
    }

    /**
     * 分页查询会员信息
     *
     * @param pageRequest
     * @return
     */
    @Override
    public Page<BaseMemberLoginInfoEntity> queryPageInfo(PageRequest pageRequest) {
        if (pageRequest == null) {
            pageRequest = PageRequest.of(0, 10);
        }
        return baseMemberLoginInfoRepository.findAll(pageRequest);
    }

    /**
     * 根据手机号查询会员是否存在
     *
     * @param memberPhone
     * @return
     */
    @Override
    public BaseMemberLoginInfoEntity querBaseMemberLoginInfo(String memberPhone) {
        if (StringUtils.isEmpty(memberPhone)) {
            return null;
        }
        BaseMemberLoginInfoEntity baseMemberLoginInfoEntity = BaseMemberLoginInfoEntity.builder().memberPhone(memberPhone).build();
        Optional<BaseMemberLoginInfoEntity> res = baseMemberLoginInfoRepository.findOne(Example.of(baseMemberLoginInfoEntity));
        if (res.isPresent()) {
            return res.get();
        }
        return null;
    }
}
